package org.spring.batch.ch08;

import java.util.HashMap;
import java.util.Map;

import org.springframework.batch.core.partition.support.Partitioner;
import org.springframework.batch.item.ExecutionContext;

/**
 * 该类的主要目的是将数据源分给不同的线程处理
 * 
 * @author wangjie
 * 
 */
public class JobPartitioner implements Partitioner {

	private static final String PARTITION_KEY = "partition-";

	@Override
	public Map<String, ExecutionContext> partition(int gridSize) {
		Map<String, ExecutionContext> result = new HashMap<String, ExecutionContext>();

		for (int i = 1; i <= gridSize; i++) {
			int begin = (i - 1) * 10 + 1;
			int end = i * 10;
			ExecutionContext value = new ExecutionContext();
			value.put("min", begin);
			value.put("max", end);
			value.put("partition", PARTITION_KEY + i);
			result.put(PARTITION_KEY + i, value);
		}
		return result;
	}

}